package com.ruoyi.common.utils.sms.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.ruoyi.common.utils.sms.ISmsSender;
import com.ruoyi.common.utils.sms.bean.Sms;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * @description: 阿里云短信接口实现类
 * @author: kubilewang
 * @date: 2022/9/7 9:51
 * @param:
 * @param: null
 * @Return: null
 * <p>
 * https://next.api.aliyun.com/api/Dysmsapi/2017-05-25/SendSms?spm=5176.25163407.quickstart-index-d6f48_f4003_0.d_65e2c_8da05_2.5370bb6e66Nt6C&params={%22SignName%22:%22%E9%98%BF%E9%87%8C%E4%BA%91%E7%9F%AD%E4%BF%A1%E6%B5%8B%E8%AF%95%22,%22TemplateCode%22:%22SMS_154950909%22,%22PhoneNumbers%22:%2217603865171%22,%22TemplateParam%22:%22%7B%22code%22%3A%221234%22%7D%22}&lang=JAVA&sdkStyle=dara
 **/
@Slf4j
@Component
public class AliSmsImpl implements ISmsSender {

    @Value("${sms.ali.accessKeyId}")
    String accessKeyId;
    @Value("${sms.ali.accessKeySecret}")
    String accessKeySecret;
    @Value("${sms.ali.signName}")
    String signName;
    @Value("${sms.ali.templateCode}")
    String templateCode;


    /**
     * 发送短信带参数
     *
     * @param sms 短信对象
     * @return boolean
     */
    @Override
    public SendSmsResponseBody send(Sms sms) throws Exception {

        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";

        Client client = new Client(config);

        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(sms.getTel())
                .setTemplateParam("{\"code\":'"+sms.getCode()+"'}");

        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);

        SendSmsResponseBody body = sendSmsResponse.getBody();

        boolean resultOk = "OK".equals(body.getCode());
        String msg=body.getMessage();

        return body;
    }
}
